<template>
  <div>
    <!-- 导航条区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type='primary'
                     @click='addDialog'>商品分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <treetable :data="cateList"
                 :columns="columns"
                 border
                 :selection-type='false'
                 :expand-type='false'
                 show-index>
        <!-- 是否有效 -->
        <template slot="isOk"
                  slot-scope="scope">
          <i class="el-icon-error"
             v-if="scope.catdeleted===false"
             style="color:lightgreen"></i>
          <i class="el-icon-success"
             v-else
             style="color:lightgreen"></i>
        </template>
        <!-- 等级 -->
        <template slot="level"
                  slot-scope="scope">
          <el-tag size="mini"
                  v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini"
                  v-else-if="scope.row.cat_level===1"
                  type="success">二级</el-tag>
          <el-tag size="mini"
                  v-else
                  type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="option">
          <el-button type="primary"
                     size="mini"
                     icon="el-icon-edit">编辑</el-button>
          <el-button type="danger"
                     size="mini"
                     icon="el-icon-delete">删除</el-button>
        </template>
      </treetable>
    </el-card>
    <el-dialog title="提示"
               :visible.sync="adddialogVisible"
               width="50%"
               style="line-height:40px"
               @close="addDialogclose">
      <!-- 添加表单 -->
      <el-form ref="addCateformref"
               :model="addCateform"
               label-width="100px"
               :rules="addCateformrule">
        <el-form-item label="分类名称:"
                      prop="cat_name">
          <el-input v-model="addCateform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader v-model="value"
                       :options="parentList"
                       :props="cascaderdata"
                       @change="handleChange"
                       :clearable='true'></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      cateList: [],
      adddialogVisible: false,
      cateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template',
        template: 'isOk'
      }, {
        label: '等级',
        type: 'template',
        template: 'level'
      }, {
        label: '操作',
        type: 'template',
        template: 'option'
      }],
      addCateform: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateformrule: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentList: [],
      cascaderdata: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      value: []
    }
  },
  created () {
    this.getCatelist()
  },
  methods: {
    getCatelist: function () {
      const that = this
      this.$http.get('categories', this.cateInfo).then(
        function (res) {
          if (res.data.meta.status !== 200) {
            that.$message.error('获取商品列表失败')
          }
          that.cateList = res.data.data
          that.total = res.data.total
        })
    },
    // 监听pagesize
    handleSizeChange: function (newsize) {
      this.cateInfo.pagesize = newsize
      this.getCatelist()
    },
    // 监听pagenum的改变
    handleCurrentChange: function (newpage) {
      this.cateInfo.pagenum = newpage
      this.getCatelist()
    },
    addDialog: function () {
      this.getParentlist()
      this.adddialogVisible = true
    },
    getParentlist: function () {
      const that = this
      this.$http.get('categories', { params: { type: 2 } }).then(
        function (res) {
          if (res.data.meta.status !== 200) {
            that.$message.error('获取列表失败')
          }
          that.parentList = res.data.data
        })
    },
    handleChange: function () {
      console.log(this.value)
      // 大于0有父级元素被选中
      if (this.value.length > 0) {
        // 父级id
        this.addCateform.cat_pid = this.value[this.value.length - 1]
        this.addCateform.cat_level = this.value.length
      } else {
        this.addCateform.cat_pid = 0
        this.addCateform.cat_level = 0
      }
    },
    // 清空对话框中的列表
    addDialogclose: function () {
      this.$refs.addCateformref.resetFields()
      this.value = []
      this.addCateform.cat_pid = 0
      this.addCateform.cat_level = 0
    },
    // 确认添加商品分类
    addConfirm: function () {
      console.log(this.addCateform)
      this.$refs.addCateformref.validate(vaild => {
        if (!vaild) return
        const that = this
        this.$http.post('categories', this.addCateform).then(
          function (res) {
            if (res.data.meta.status !== 201) {
              that.$message.error('添加商品失败')
            }
            that.$message.success('添加商品成功')
            that.getCatelist()
            that.adddialogVisible = false
          })
      })
    }
  }
}
</script>
<style lang="less" scoped>
.el-card {
  line-height: 60px;
}
.el-cascader {
  width: 100%;
}
</style>
